// pages/activity/invite/invite.js
import api from '../../../utils/api';
import message from '../../../utils/message'


Page({

    /**
     * 页面的初始数据
     */
    data: {

        has_load:false,
        acitity:{
            title:'邀请新用户注册，领10元现金红包',
            id:10,
        },
        invite_id:7,  //邀请人ID

        activity_info:null,
        user_info:null,
        is_userinfo:false,
        share_info:{
            title:'风云锦玉邀请你注册农夫农夫花园',
            imageUrl:'https://www.hujingx.com/web/static/images/ppp.png',
            path:'/pages/activity/invite/invite?activity_id=8&invite_id=7&type=accept'
        },
        type:'invite',
        is_invite:true,
        invite_user_list:[],
        is_show_qrcode:false
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {

        console.log(options);

        if(options.type){

            this.setData({
                type:options.type
            })
        }else{
            this.setData({
                type:'detail'
            })
        }

        if(options.activity_id  && options.user_id ){
            this.getActivityInfo( options.activity_id , options.user_id )
        }
        wx.showShareMenu({
            withShareTicket:true,
            menus: ["shareAppMessage" ,"shareTimeLine"],
        })

    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {


        this.getActivityInfo( 8 , 7);

        var user_info = wx.getStorageSync("user_info" , false);

        var result = JSON.parse(user_info);
        console.log(result)

        if(!result){

            this.setData({
                user_info:null,
                is_userinfo:false,
            })
            
        }else{

            this.setData({
                "user_info":result,
                is_userinfo:true,
            })
            
        }

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

        var share_info = this.data.share_info;
        console.log(share_info);
        if(share_info){
            var options = {

                title:share_info.title,
                imageUrl:share_info.imageUrl,
                path: share_info.path,
                success:function( res ){
                    if(res.errMsg == 'shareAppMessage:ok'){
                        message.toast("分享成功");
                    }
                },
                fail:function( res ){

                    console.log(res);
                    if(res.errMsg == 'shareAppMessage:fail cancel'){

                        message.toast("取消转发")
                    }

                    if(res.errMsg == 'shareAppMessage:fail'){
                       // 转发失败，其中 detail message 为详细失败信息
                       message.toast("转发失败");
                       console.log(res);
                    }
                },
                complete:function(){
                    console.log("分享完成时调用");
                }
            }
            return options;
        }
    },
    onShareTimeline:function(){
        //微信朋友全分享设置


    },
    getActivityInfo:function( activity_id , user_id ){
        var me = this;
        var params = {}
        params.activity_id = activity_id;
        params.user_id = user_id



        message.show_loading();
        api.getActivityInfo( params, function(response){
            message.hide_loading();

            console.log(response);

            me.setData({
                activity_info:response.data.activity,
                user_info:response.data.user_info,
                has_load:true,
                invite_user_list:response.data.invite_user_list
            })

        })

    },

    doActivityHandler:function(){
        //参与活动

        var params = {};
        params.activity_id = 10;
        api.doActivityHandler( params  , function( response ){
            if(response.status==99){
                wx.redirectTo({
                  url: '/pages/login/login',
                })

            }

        })
    },
    getPacketMoeny:function(){
        //参与活动  获得奖励  通用类型，根据后台设置得活动数据，执行相关操作

    },
    doAcceptInvite:function(){
        //接受邀请
        //验证用户是否登录，
        //如果没登录，提示用户登录

        console.log("提示登录");

        console.log( this.data.is_userinfo );

        var params = {};

        //校验用户是否登录
    
        //api.activtity.acceptInvite()

        /*

        if(!this.data.is_userinfo){
            //提示用户未登录
            message.show_error("您还未登录，请先登录" , function(){
                wx.navigateTo({
                    url: '/pages/login/login',
                });
            } , "确定")
        }

        */

        params.activity_id = 8;
        params.invite_id = 9;

        api.activity.acceptInvite(  params , function( response ){
            console.log(response)
            if(response.staus==1){

            }else{

            }
        })
    },
    doShowQrcode:function( e ){

        var action = e.currentTarget.dataset.action;

        if(action==1){
            this.setData({
                is_show_qrcode: true
            })

        }
        if(action==-1){
            this.setData({
                is_show_qrcode: false
            })
        }



    },
    getInviteInfo:function(){
        
    }
})